package com.easy.tool.utils.uuid;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.easy.tool.anno.BeanDes;
import com.easy.tool.anno.MethodDes;
import com.easy.tool.constant.DateConstant;
import com.easy.tool.utils.date.DateUtils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author 高俊
 * @create 2021-08-2021/8/27-9:58
 */
@BeanDes(name = "随机数工具",des = "用于生成各种类型的随机数")
public class UUIDUtils {

    private static String [] allLetter = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","l","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","W","X","Y","Z"};
    private static String [] number = {"0","1","2","3","4","5","6","7","8","9"};
    private static String [] letter = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","l","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","W","X","Y","Z"};
    private static String [] symbol = {"!","~","@","#","$","%","^","&","*","-","_","=","+","/","<",">"};

    /**
     * 生成随机位数字符串 0~9,a~z,A~Z
     * @param digit 位数
     * @return
     */
    @MethodDes(des = "生成随机位数字符串 0~9,a~z,A~Z",param = {"arg0-> digit：位数"})
    public static String getAllLetterRandom(int digit){
        String str = "";
        for (int i = 0; i < digit; i++) {
            int index = getRangeRandom(61, 0);
            str = str + allLetter[index];
        }
        return str;
    }

    /**
     * 生成随机位字母符串 a~z,A~Z
     * @param digit 位数
     * @return
     */
    @MethodDes(des = "生成随机字母字符串 a~z,A~Z",param = {"arg0-> digit：位数"})
    public static String getLetterRandom(int digit){
        String str = "";
        for (int i = 0; i < digit; i++) {
            int index = getRangeRandom(letter.length, 0);
            str = str + letter[index];
        }
        return str;
    }

    /**
     * 生成随机位数字符串 0~9
     * @param digit 位数
     * @return
     */
    @MethodDes(des = "生成随机位数字符串 0~9",param = {"arg0-> digit：位数"})
    public static String getNumberRandom(int digit){
        String str = "";
        for (int i = 0; i < digit; i++) {
            int index = getRangeRandom(number.length, 0);
            str = str + number[index];
        }
        return str;
    }

    /**
     * 生成特殊字符串
     * @param digit 位数
     * @return
     */
    @MethodDes(des = "生成特殊字符串",param = {"arg0-> digit：位数"})
    public static String getSymbolRandom(int digit){
        String str = "";
        for (int i = 0; i < digit; i++) {
            int index = getRangeRandom(symbol.length, 0);
            str = str + symbol[index];
        }
        return str;
    }

    /**
     * 添获取自定义随机数id
     * @param digit 位数
     * @param bool  是否加上时间标识
     * @return
     */
    @MethodDes(des = "添获取自定义随机数id",param = {"arg0-> digit：位数","arg1-> bool：是否加上时间标识"})
    public static String getRandom(int digit,boolean bool){
        String id = "";
        // 如果传入为0 则默认为10位数
        if (digit == 0){
            digit = 10;
        }
        if (bool){
            id += DateUtils.dateFormat(new Date(), DateConstant.GET_STRING_SHOR_TTIME);
        }
        Random random = new Random();
        for (int i=0;i<digit;i++){
            int a = random.nextInt(10);
            id += a;
        }
        return id;

    }

    /**
     * 随机UUID
     * @param digit uuid位数 0:不截取
     * @param bool  是否携带 “-” true：携带：false：不携带
     * @return  String类型UUID
     */
    @MethodDes(des = "随机UUID",param = {"arg0-> digit：uuid位数 0:不截取","arg1-> bool：是否携带 “-” true：携带：false：不携带"})
    public static String getUuidString(int digit,boolean bool){
        String uuid = UUID.randomUUID().toString();
        if (digit > 0 && digit <= uuid.length()){
            uuid.substring(0,digit);
        }
        if (!bool){
            uuid.replaceAll("-", "");
        }
        return uuid;
    }

    /**
     * 生成指定范围中的随机数
     * @param max 最大值
     * @param min 最小值
     * @return
     */
    @MethodDes(des = "生成指定范围中的随机数",param = {"arg0-> max：最大值","arg1-> min：最小值"})
    public static int getRangeRandom(int max,int min){
        Random random = new Random();
        int i = random.nextInt(max - min) + min;
        return i;
    }


    /**
     * 生成指定范围中的随机数  Long类型
     * @return
     */
    @MethodDes(des = "生成指定范围中的随机数  Long类型",param = {"arg0-> max：最大值","arg1-> min：最小值"})
    public static Long getLongRangeRandom(int max,int min){
        Random random = new Random();
        int i = random.nextInt(max - min) + min;
        return Long.parseLong(i + "");
    }

    /**
     * 生成nanoid
     * @return
     */
    @MethodDes(des = "生成nanoid  String类型")
    public static String getNanoid(){
        return NanoIdUtils.randomNanoId();
    }
}
